package com.genreate.util;

import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import com.genreate.util.StringUtils;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**
 * <p>描述: [拼音工具类] </p>
 * <p>创建时间: 2020/6/1 10:08 </p>
 *
 * @author sgq
 * @version v1.0.0
 */
public class PinYinUtil {
    /**
     * 正则表达式
     */
    public static final String CHINESE_REGEX = "[\u4e00-\u9fa5\\w]+";
    /**
     * 表字段最大长度
     */
    public static final int TABLE_FIELD_MAX_LENGTH = 32;

    /**
     * 判断传入的字符串：表名或者字段名是否满足正确的格式 -汉字英文数字下划线
     *
     * @param str
     * @return
     */
    public static boolean matchISatisfied(String str) {
        if (StringUtils.isBlank(str)) {
            return false;
        }
        str = str.trim();
        if (str.length() > TABLE_FIELD_MAX_LENGTH) {
            return false;
        }
        if (ReUtil.isMatch(CHINESE_REGEX, str)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判断传入的字符串；不符合抛异常
     *
     * @param str str
     */
    public static void matchISatisfiedThrow(String str) {
        if(!matchISatisfied(str)){
            throw new RuntimeException("请输入正确的格式【汉字_英文_数字_下划线】");
        }
    }

    public static void main(String[] args) {
        String s = "大所发生的dsf13_2";
        boolean b = matchISatisfied(s);
        System.out.println();
    }

    /**
     * <p>描述: 将汉字转换为全拼 </p>
     *
     * @param src 汉字
     * @return java.lang.String
     * @author sgq
     */
    public static String getPingYin(String src) {
        if (StrUtil.isBlank(src)){
            return "";
        }
        src = src.trim();
        char[] t1;
        t1 = src.toCharArray();
        String[] t2;
        HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();

        t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        t3.setVCharType(HanyuPinyinVCharType.WITH_V);
        String t4 = "";
        int t0 = t1.length;
        try {
            for (int i = 0; i < t0; i++) {
                // 判断是否为汉字字符
                if (Character.toString(t1[i]).matches(
                        "[\\u4E00-\\u9FA5]+")) {
                    t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
                    t4 += t2[0];
                } else {
                    char word = t1[i];
                    // 大写转小写的
                    if (word >= 'A' && word <= 'Z') {
                        word += 32;
                    }
                    t4 += Character.toString(word);
                }
            }
            return t4;
        } catch (BadHanyuPinyinOutputFormatCombination e1) {
            e1.printStackTrace();
        }
        return t4;
    }

    /**
     * <p>描述:  返回中文的首字母</p>
     *
     * @param str 汉字
     * @return java.lang.String
     * @author sgq
     */
    public static String getPinYinHeadChar(String str) {
        if (StrUtil.isBlank(str)){
            return "";
        }
        str = str.trim();
        String convert = "";
        for (int j = 0; j < str.length(); j++) {
            char word = str.charAt(j);
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
            if (pinyinArray != null) {
                convert += pinyinArray[0].charAt(0);
            } else {
                // 大写转小写的
                if (word >= 'A' && word <= 'Z') {
                    word += 32;
                }
                convert += word;
            }
        }
        return convert;
    }
}
